﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong_Plus
{
    class bola
    {
        Texture2D texture;
        Vector2 position;
        public Rectangle size;
        Vector2 velocity;
        Random random = new Random();
        SoundEffect soundEffect;

        public bola(GameWindow janela, Texture2D texture, Vector2 position, SoundEffect soundEffect)//<<<<CONSTRUTOR!!!
        {
            this.texture = texture;//AQUI
            this.size = texture.Bounds;//AQUI
            this.position = new Vector2(
            janela.ClientBounds.Width / 2
            - this.size.Width / 2,
            janela.ClientBounds.Height / 2
            - this.size.Height / 2);//AQUI
            //Faz com randimico
            float posicaoX = random.Next(-3,3);
            float posicaoY = random.Next(-3,3);
            this.velocity = new Vector2(posicaoX, posicaoY);//AQUI
            this.soundEffect = soundEffect;
            

        }

        public void Update(GameTime gameTime, GameWindow janela)
        {

            position += velocity;
            ManterNaTela(janela);

        }

        public void ManterNaTela(GameWindow janela)
        { 
            //Fazendo o quique da bola -- Trocando de sentindo cada vez que a mesma toca o limite da tela
            if (position.Y <= 0)
            {
                velocity.Y = -velocity.Y;
                soundEffect.Play();
            }

            if (position.X <= 0)
            {
                velocity.X *= -1;
                soundEffect.Play();
            }

            if (position.Y >= (janela.ClientBounds.Height - texture.Height))
            {
                velocity.Y *= -1;
                soundEffect.Play();
            }

            if (position.X >= (janela.ClientBounds.Width - texture.Width))
            {
                velocity.X *= -1;
                soundEffect.Play();
            }
        
        }

        //Update da colisao
        public void Update(GameTime gameTime, GameWindow janela, paleta paletaColisao)
        {

            position += velocity;
            colisao(paletaColisao);

            this.size.X = (int)position.X;
            this.size.Y = (int)position.Y;

        }

        public void colisao(paleta paletaColisao)
        {

            if (this.size.Intersects(paletaColisao.size))
            {
          
                velocity.X *= -1;
                soundEffect.Play();
            }

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);

        }
        
    }

}
